<template>
  <div class="user-box">
    <el-button size="mini" @click="dialogVisible = true">新增</el-button>

    <el-dialog
      title="新增"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <el-form
        :rules="rules"
        :inline="true"
        ref="form"
        :model="form"
        label-width="80px"
      >
        <!-- 姓名 -->
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <!-- 年龄 -->
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age"></el-input>
        </el-form-item>
        <!-- 性别 -->
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" :value="0"></el-option>
            <el-option label="女" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <!-- 出生日期 -->
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="form.birth"
          ></el-date-picker>
        </el-form-item>

        <!-- 地址 -->
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit()"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 表格 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="sex" label="性别" width="180"> 
        
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="180"> </el-table-column>
      <el-table-column prop="birth" label="出生日期" width="180"> </el-table-column>
      <el-table-column prop="addr" label="地址" width="180"> </el-table-column>
      <el-table-column  label="操作" width="180">
        <template slot-scope="scope">
        <el-button
          size="mini"
          @click="handleEdit(scope.row)">编辑</el-button>
        <el-button
          size="mini"
          type="danger"
          @click="handleDelete()">删除</el-button>
      </template>
      </el-table-column>

    </el-table>

  </div>
</template>
<style scoped>
</style>

<script>
import { getUser,addUser,editUser } from '../api/index.js';
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        name: "",
        age: "",
        sex: "",
        birth: "",
        addr: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        age: [{ required: true, message: "请输入年龄", trigger: "blur" }],
        sex: [{ required: true, message: "请输入性别", trigger: "blur" }],
        birth: [{ required: true, message: "请输入出生日期", trigger: "blur" }],
        addr: [{ required: true, message: "请输入地址", trigger: "blur" }],
      },
      tableData:[],
      // 对话框的状态
      modelType:0
    };
  },
  methods:{
    // 点击新增确定
    submit(){
      this.$refs.form.validate((val)=>{
        if(val){
          // console.log(this.form);
          if(this.modelType=== 0){
            addUser(this.form).then(()=>{
              // 重新获取数据
              this.getList()
            })
          }else{
            editUser(this.form).then(()=>{
              // 重新获取数据
              this.getList()
            })
          }



        }




      })



      // 重置表单
      this.$refs.form.resetFields()
      // 关闭弹窗
      this.dialogVisible=false
    },
    // 关闭
    handleClose(){
            // 重置表单
            this.$refs.form.resetFields()
      // 关闭弹窗
      this.dialogVisible=false
    },
    // 编辑

    handleEdit(row){
      this.modelType=1
      this.dialogVisible=true
      // 修改数据，不能修改原数据，可以copy下来再修改
      this.form=JSON.parse(JSON.stringify(row))
    },

    // 获取列表数据
    getList(){
      getUser().then(({data})=>{
        //  console.log(data);
        this.tableData=data.list
        // console.log( this.tableData);
      })
    }

  },
  mounted(){
    this.getList()
  }



};
</script>